package com.app.service.impl;

import com.app.model.CommentFileModel;
import com.app.mapper.CommentFileMapper;
import com.app.model.FileModel;
import com.app.service.CommentFileService;
import com.app.utils.CommonHelp;
import com.app.utils.CommonUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 16693103420
 * @since 2024-01-23
 */
@Service
public class CommentFileServiceImpl extends ServiceImpl<CommentFileMapper, CommentFileModel> implements CommentFileService {
    @Autowired
    private CommentFileMapper commentFileMapper;
    //将图片或者视频路径列表转化为ID列表
    @Override
    public StringBuilder uriListToIdList(StringBuilder idList, List<String> urlList) {
        int count=0;
        for (String url : urlList) {
            //获取文件名称fileName
            String[] split = url.split("/");
            String fileName = split[split.length - 1];
            //获取URL后缀名urlSuffix
            int lastIndexOf = url.lastIndexOf(".");
            String urlSuffix = url.substring(lastIndexOf + 1);

            CommentFileModel commentFile = new CommentFileModel();
            commentFile.setName(fileName);
            commentFile.setUrl(url);
            Integer fileType = CommonUtil.getFileType(urlSuffix);
            commentFile.setFileType(fileType);

            commentFile.setCreateTime(CommonHelp.getNowTimestamp());
            commentFileMapper.insert(commentFile);
            if (count == urlList.size()-1) {
                idList.append(commentFile.getId());
            } else {
                idList.append(commentFile.getId()).append(",");
            }
            count++;
        }
        return idList;
    }
}
